#include "EEntity.h"
#include "eCPP.h"

namespace engine
{

//------------------------------------------------------------
EEntity::EEntity( _eGroupType eGroupType ) :
m_pOgreEntity( NULL ),
m_bIsVisible( false ),
m_sName( "<unknown>" ),
m_pOgreAnimationState( NULL ),
m_pOgreNode( NULL ),
m_vPosition( 0.f, 0.f, 0.f ),
m_pOgreSceneManager( NULL ),
m_vScale( 1.f ),
m_bCastShadows( true )
{
}

//------------------------------------------------------------
EEntity::EEntity( _eGroupType eGroupType, const string& sName ) :
m_pOgreEntity( NULL ),
m_bIsVisible( false ),
m_sName( sName ),
m_pOgreAnimationState( NULL ),
m_pOgreNode( NULL ),
m_vPosition( 0.f, 0.f, 0.f ),
m_pOgreSceneManager( NULL ),
m_vScale( 1.f ),
m_bCastShadows( true )
{

}

//------------------------------------------------------------
EEntity::EEntity( _eGroupType eGroupType, const string& sName, const string& sModelFileName) :
m_pOgreEntity( NULL ),
m_bIsVisible( true ),
m_sName( sName ),
m_sModelFileName( sModelFileName ),
m_pOgreAnimationState( NULL ),
m_pOgreNode( NULL ),
m_vPosition( 0.f, 0.f, 0.f ),
m_pOgreSceneManager( NULL ),
m_vScale( 1.f ),
m_bCastShadows( true )
{

}

//------------------------------------------------------------
bool EEntity::load( SceneManager& sceneManager )
{
  m_pOgreEntity = sceneManager.createEntity( m_sName, m_sModelFileName );
  cTEST( m_pOgreEntity != NULL );
  m_pOgreEntity->setCastShadows( m_bCastShadows );
  m_pOgreNode   = sceneManager.getRootSceneNode()->createChildSceneNode( getNodeName() );
  cTEST( m_pOgreNode != NULL );
  m_pOgreNode->attachObject( m_pOgreEntity );
  m_pOgreNode->setPosition( m_vPosition.x, m_vPosition.y, m_vPosition.z );
  m_pOgreSceneManager = &sceneManager;

  return true;
}

//------------------------------------------------------------
string EEntity::getNodeName() const
{
  return m_sName + "Node";
}

//------------------------------------------------------------
void EEntity::setPosition( const EVector& vPos )
{
  m_vPosition = vPos;
  if( m_pOgreNode != NULL )
  {
    m_pOgreNode->setPosition( vPos.x, vPos.y, vPos.z );
  }
}

//------------------------------------------------------------
void EEntity::setPosition(float fX, float fY, float fZ )
{
  m_vPosition = EVector( fX, fY, fZ );
  if( m_pOgreNode != NULL )
  {
    m_pOgreNode->setPosition( fX, fY, fZ );
  }
}

//------------------------------------------------------------
void EEntity::setScale(const EVector& vScale )
{
  m_vScale = vScale;
  if( m_pOgreNode != NULL )
  {
    m_pOgreNode->setScale( vScale.x, vScale.y, vScale.z );
  }
}

//------------------------------------------------------------
void EEntity::setScale( float fSX, float fSY, float fSZ )
{
  m_vScale = EVector( fSX, fSY, fSZ );
  if( m_pOgreNode != NULL )
  {
    m_pOgreNode->setScale( fSX, fSY, fSZ );
  }
}

//------------------------------------------------------------
void EEntity::setScale(float fSXYZ )
{
  m_vScale = EVector( fSXYZ );
  if( m_pOgreNode != NULL )
  {
    m_pOgreNode->setScale( fSXYZ, fSXYZ, fSXYZ );
  }
}

//------------------------------------------------------------
bool EEntity::update( const EGameTime& time )
{
  if( m_pOgreAnimationState != NULL )
  {
    m_pOgreAnimationState->addTime( time.m_fDT );
  }
  return true;
}

//------------------------------------------------------------
bool EEntity::setAnimationState( const string& sState, bool bLoop )
{
  cASSERT_STRING( m_pOgreEntity != NULL, "No entity loaded" );
  AnimationState* pState = m_pOgreEntity->getAnimationState( sState );
  cTEST( pState != NULL );

  m_pOgreAnimationState = pState;
  m_pOgreAnimationState->setLoop( bLoop );

  return true;
}

//------------------------------------------------------------
void EEntity::enableAnimation( bool bEnabled )
{
  if( m_pOgreAnimationState == NULL )
    return;
  m_pOgreAnimationState->setEnabled( bEnabled );
}

//------------------------------------------------------------
const MeshPtr& EEntity::getOgreMesh() const
{
  cASSERT( m_pOgreEntity != NULL );
  return m_pOgreEntity->getMesh();
}
} // end namespace